package com.yuyou.train.gateway;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yuyou.train.util.bean.ReturnCode;
import com.yuyou.train.util.bean.ReturnResult;
import com.yuyou.train.util.exception.CheckException;

@ControllerAdvice
public class ExceptionHandle {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());  
    
    @ExceptionHandler(value = CheckException.class)
    public void defCheckException(CheckException e) throws CheckException{    	
    	logger.error("defFeignException:",e);
    	throw new CheckException("CheckException:",e);
    }
    
    /**
     * 判断错误是否是已定义的已知错误，不是则由未知错误代替，同时记录在log中
     * @param e
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ReturnResult allException(Exception e){    	 
    	logger.error("未捕捉异常UnknownError:",e);
        return ReturnResult.fail("服务器异常",e.toString(),ReturnCode.code._2001.getCode(),ReturnCode.code._2001.getMessage());
    }
    

    
    
}
